package com.itheima.sevice.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.sevice.CheckGroupService;
import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.exception.DeletionNotAllowedException;
import com.itheima.dao.CheckGroupCheckItemDao;
import com.itheima.dao.CheckGroupDao;
import com.itheima.dao.SetmealCheckGroupDao;
import com.itheima.pojo.CheckGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupDao checkGroupDao;

    @Autowired
    private CheckGroupCheckItemDao checkGroupCheckItemDao;

    @Autowired
    private SetmealCheckGroupDao setmealCheckGroupDao;

    /**
     * 根据id查询检查组
     * @param id
     * @return
     */
    @Override
    public CheckGroup findById(Integer id) {
        return checkGroupDao.findById(id);
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<CheckGroup> checkGroups = checkGroupDao.findPage(queryPageBean);

        return new PageResult(checkGroups.getTotal(), checkGroups.getResult());
    }

    /**
     * 新增检查组
     * @param checkitemIds
     * @param checkGroup
     * @return
     */
    @Override
    @Transactional
    public Integer add(List<Integer> checkitemIds, CheckGroup checkGroup) {
        Integer recordCount = checkGroupDao.add(checkGroup);
        Integer checkGroupId = checkGroup.getId();

        if (checkitemIds != null && checkitemIds.size() > 0){
            for (Integer checkitemId : checkitemIds) {
                checkGroupCheckItemDao.add(checkGroupId, checkitemId);
            }
        }
        return recordCount;
    }

    /**
     * 根据id删除检查组
     * @param id
     * @return
     */
    @Override
    @Transactional
    public Integer deleteById(Integer id) {
        // 套餐和检查组有关联，则不能删除该检查项
        Integer count = setmealCheckGroupDao.countByCheckGroupId(id);
        if (count != null && count > 0){
            throw new DeletionNotAllowedException(MessageConstant.DELETE_CHECKITEM_FAIL);
        }

        // 清除检查组-检查项表中的记录
        checkGroupCheckItemDao.deleteByCheckGroupId(id);

        Integer recordCount = checkGroupDao.deleteById(id);
        return recordCount;
    }

    /**
     * 根据套餐id查询关联的检查项id
     * @param setmealId
     * @return
     */
    @Override
    public List<Integer> findCheckGroupIdsBySetmealId(Integer setmealId) {

        return setmealCheckGroupDao.findCheckGroupIdsBySetmealId(setmealId);
    }

    /**
     * 编辑检查组
     * @param checkitemIds
     * @param checkGroup
     * @return
     */
    @Override
    @Transactional
    public Integer update(List<Integer> checkitemIds, CheckGroup checkGroup) {
        // 清除当前检查组中的多有检查项
        checkGroupCheckItemDao.deleteByCheckGroupId(checkGroup.getId());

        // 更新当前的检查组
         Integer recordCount = checkGroupDao.update(checkGroup);

        // 更新检查组中的检查项
        if (checkitemIds != null && checkitemIds.size() > 0){
            for (Integer checkitemId : checkitemIds) {
                checkGroupCheckItemDao.add(checkGroup.getId(), checkitemId);
            }
        }
        return recordCount;
    }

    /**
     * 查询全部检查组
     * @return
     */
    @Override
    public List<CheckGroup> findAll() {

        return checkGroupDao.findAll();
    }
}
